#ifndef _DX8_BUFFER_H_
#define _DX8_BUFFER_H_

#include <d3d8.h>
#include <vector>

class	GraphicsBuffer
{
public:
	GraphicsBuffer(unsigned int Count, unsigned int StepSize):
		m_TotalSize(Count*StepSize),
		m_Count(Count),
		m_StepSize(StepSize)	{		}
	virtual ~GraphicsBuffer()	{		}
	virtual void				Load(void* pData, int Count) = 0;

protected:
	unsigned int				m_TotalSize;
	unsigned int				m_Count, m_StepSize;
};

class	GraphicsBufferSet
{
public:
	GraphicsBufferSet(int Count, unsigned int MaxCount, unsigned int StepSize) :
		m_MaxCount(MaxCount),
		m_LastBuffer(0)
	{

	}
	virtual ~GraphicsBufferSet()
	{
		for (unsigned int i=0; i<m_Buffers.size(); i++)
			delete m_Buffers[i];
	}
	GraphicsBuffer*						GetBuffer() { if (m_Buffers.empty()) return NULL; return m_Buffers[ (m_LastBuffer++) % m_Buffers.size()]; };
	bool								CanFit(int Count) {  return Count < m_MaxCount; };
protected:
	std::vector<GraphicsBuffer*>		m_Buffers;
	int									m_LastBuffer;
	int									m_MaxCount;
};


class	DX8VertexBuffer : public GraphicsBuffer
{
public:
	DX8VertexBuffer(unsigned int Count, unsigned int StepSize, unsigned int FVF);
	~DX8VertexBuffer();
	virtual void				Load(void* pData, int Count);
	LPDIRECT3DVERTEXBUFFER8		GetBuffer() { return m_pBuffer; };
private:
	unsigned int				m_FVF;
	LPDIRECT3DVERTEXBUFFER8		m_pBuffer;
};

class	DX8IndexBuffer : public GraphicsBuffer
{
public:
	DX8IndexBuffer(unsigned int Count);
	~DX8IndexBuffer();
	virtual void				Load(void* pData, int Count);
	LPDIRECT3DINDEXBUFFER8		GetBuffer() { return m_pBuffer; };
private:
	unsigned int				m_FVF;
	LPDIRECT3DINDEXBUFFER8		m_pBuffer;
};

class	DX8VBSet : public GraphicsBufferSet
{
public:
	DX8VBSet(int Count, unsigned int MaxCount, unsigned int StepSize, unsigned int FVF) :
	  GraphicsBufferSet(Count, MaxCount, StepSize)
	{
		for (int i = 0; i<Count; i++)
		{
			m_Buffers.push_back(new DX8VertexBuffer(MaxCount, StepSize, FVF));
		}
	}

};

class	DX8IBSet : public GraphicsBufferSet
{ 
public:

	DX8IBSet(int Count, unsigned int MaxCount) :
	  GraphicsBufferSet(Count, MaxCount, sizeof(unsigned short))
	{
		for (int i = 0; i<Count; i++)
		{
			m_Buffers.push_back(new DX8IndexBuffer(MaxCount));
		}
	}
};

#endif // _DX8_BUFFER_H_